//
//  SpaceCargoShip.m
//  LearningCocos
//
//  Created by abyssos on 2014. 3. 5..
//  Copyright (c) 2014년 ainhan. All rights reserved.
//

#import "SpaceCargoShip.h"

@implementation SpaceCargoShip

@synthesize delegate;

- (void)dropCargo {
    CGPoint cargoDropPosition = ccp(screenSize.width / 2, screenSize.height);
    if(hasDroppedMallet == NO) {
        CCLOG(@"SpaceCargoShip --> Mallet Powerup was created!");
        hasDroppedMallet = YES;
        [delegate createObjectOfType:kPowerUpTypeHealth withHealth:0.0f atLocation:cargoDropPosition withZValue:50];
    }
}

- (id)initWithSpriteFrameName:(NSString *)spriteFrameName {
    
    self = [super initWithSpriteFrameName:spriteFrameName];
    if (self) {
        CCLOG(@"SpaceCargoShip init [%f x %f]", [self textureRect].size.width, [self textureRect].size.height);
        
        screenSize = [CCDirector sharedDirector].winSize;
        
        hasDroppedMallet = NO;
        //float shipHeight = screenSize.height * 0.71f;
        float shipHeight = [self textureRect].size.height;

        
        CCLOG(@"SpaceCargoShip screenSize.width: %f, shipHeight: %f", screenSize.width, shipHeight);
        
        CGPoint position1 = ccp(screenSize.width * -0.48f, shipHeight);
        CGPoint position2 = ccp(screenSize.width * 2.0f, shipHeight);
        CGPoint position3 = ccp(position2.x * -1.0f, shipHeight);
        CGPoint offScreen = ccp(screenSize.width * -1.0f, screenSize.height * -1.0f);
        
        CGPoint position4 = ccp(1024.0f, 768.0f / 2);
        CGPoint position5 = ccp(0.0f, 0.0f);
        id action2 = [CCRepeatForever actionWithAction:[CCSequence actions:
                                                        [CCMoveTo actionWithDuration:2.0f position:position1],
                                                        [CCMoveTo actionWithDuration:2.0f position:position2],
                                                        [CCMoveTo actionWithDuration:2.0f position:position3],
                                                        //[CCMoveTo actionWithDuration:2.0f position:position4],
                                                        nil]];
        
        id action = [CCRepeatForever actionWithAction:[CCSequence actions:
                                                       [CCDelayTime actionWithDuration:10.0f],
                                                       
                                                       [CCMoveTo actionWithDuration:0.01f position:position1],
                                                       [CCScaleTo actionWithDuration:0.01f scale:0.5f],
                                                       [CCFlipX actionWithFlipX:YES],
                                                       
                                                       [CCMoveTo actionWithDuration:8.5f position:position2],
                                                       [CCScaleTo actionWithDuration:0.1f scale:1.0f],
                                                       [CCFlipX actionWithFlipX:NO],
                                                       
                                                       [CCMoveTo actionWithDuration:7.5f position:position3],
                                                       [CCScaleTo actionWithDuration:0.1f scale:2.0f],
                                                       [CCFlipX actionWithFlipX:YES],
                                                       
                                                       [CCMoveTo actionWithDuration:6.5f position:position2],
                                                       [CCFlipX actionWithFlipX:NO],
                                                       [CCScaleTo actionWithDuration:0.1f scale:2.0f],
                                                       
                                                       [CCMoveTo actionWithDuration:5.5f position:position3],
                                                       [CCFlipX actionWithFlipX:YES],
                                                       [CCScaleTo actionWithDuration:0.1f scale:4.0f],

                                                       [CCMoveTo actionWithDuration:4.5f position:position2],
                                                       
                                                       [CCCallFunc actionWithTarget:self selector:@selector(dropCargo)],
                                                       [CCMoveTo actionWithDuration:0.0f position:offScreen],
                                                       nil]
                    ];
        [self runAction:action];
    }
    return self;
}


@end
